#include "../main.h"

#ifndef FANCTRL_H
#define FANCTRL_H

//初始化调用: FanCtrlInit();
//主循环调用: 
//  HeatingFanCtrl();
//  ExhaustFanCtrl();
//  IgniterCtrl();
//定时器 Tick 10ms调用: FanCtrlClock();
//控制所有温控设备准备工作: FanCtrlStart();
//控制所有温控设备停止工作: FanCtrlStop();


#define SmokeTempSetting 165 //°F
#define HighTempSetting 450  //°F
#define SmokeTempMeatSetting 140 //°F
#define HighTempMeatSetting 215 //°F

#define FanCtrl_GPIO_PIN_SEL  ((1ULL<<IO_Heater) | (1ULL << IO_Feed) | (1ULL << IO_Fan))

typedef struct
{
    uint8_t Heater:1; //1:Open, 0:Close
    uint8_t Feed:1;
    uint8_t Fan:1;
}_Fan_State_S;
extern _Fan_State_S Fan_State;
//原理图上: 输出高电平风扇停止, 输出低电平风扇运行
#define IgniterOpen do{gpio_set_level(IO_Heater,1); Fan_State.Heater = 1;}while(0) //开始加热点火输出 (紫色线)
#define IgniterClose do{gpio_set_level(IO_Heater,0); Fan_State.Heater = 0;}while(0) //停止加热点火输出
#define HeaterFanOpen do{gpio_set_level(IO_Feed,1); Fan_State.Feed = 1;}while(0) //开始进料加热输出 (红色线)
#define HeaterFanClose do{gpio_set_level(IO_Feed,0); Fan_State.Feed = 0;}while(0) //停止进料加热输出
#define ExhaustFanOpen do{gpio_set_level(IO_Fan,1); Fan_State.Fan = 1;}while(0) //开始排气扇输出 (橙色线)
#define ExhaustFanClose do{gpio_set_level(IO_Fan,0); Fan_State.Fan = 0;}while(0) //停止排气扇输出

#define FanClose_CountDown 600 //切换到Shut Down档位后,排气扇打开持续该时间后关闭
#define FanClose_Delay_Time (FanClose_CountDown + 10) //10s,用于显示OFF的时间,闪耀4次


//以下两个参数,需要提供
extern uint16_t temp_set;
extern uint8_t Smoke_P_Value;
extern uint8_t IgniterStep;
extern uint8_t ExhaustFanStep;
extern uint16_t ExhaustFanClock; //if(ExhaustFanClock > 0) 显示OFF跟关机倒计时
extern uint16_t PowerDown_Clock; //关机倒计时 10min, Tick 1s

void FanCtrlInit(void);
void FanCtrlClock(void);
void FanCtrlStart(void);
void FanCtrlStop(uint16_t);
void FanCtrlForceStop(void);
void HeatingFanCtrl(void);
void ExhaustFanCtrl(void);
void IgniterCtrl(void);

int GetTemp(int F);

#endif
